package com.umeox.watch.moto.launcher.ui

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity
import com.umeox.watch.moto.launcher.R

/**
 * Created by Rambo 2022/2/28
 * Describe:
 */
class GuideActivity : ThemeAppCompatActivity(), ViewPager.OnPageChangeListener {

    private val TAG = GuideActivity::class.java.simpleName

    private lateinit var mViewPager: ViewPager


    private val viewContainer = ArrayList<View>()

    private var isDoneFinish: Boolean = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.guide_activity)
        isDoneFinish = intent.getBooleanExtra("is_done_finish", true)
        initView()
    }

    private fun initView() {
        mViewPager = findViewById(R.id.vp)

        val inflater = getSystemService(LAYOUT_INFLATER_SERVICE) as LayoutInflater

        initGuideView1(viewContainer, inflater)
        initGuideView2(viewContainer, inflater)
        mViewPager.adapter = ViewPagerAdapter()
        mViewPager.currentItem = 0
        mViewPager.addOnPageChangeListener(this)

    }

    private fun initGuideView1(viewList: ArrayList<View>, inflater: LayoutInflater) {
        val view: View = inflater.inflate(R.layout.layout_guide_1, null)
        view.findViewById<ImageView>(R.id.iv_next).setOnClickListener {
            mViewPager.setCurrentItem(1, true)
        }

        viewList.add(view)
    }

    private fun initGuideView2(viewContainer: ArrayList<View>, inflater: LayoutInflater) {
        val view: View = inflater.inflate(R.layout.layout_guide_2, null)
        view.findViewById<ImageView>(R.id.iv_next).setOnClickListener {
            if (isDoneFinish) {
                finish()
            } else {
                val intent = Intent(mContext,MainActivity::class.java)
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
                startActivity(intent)
                finish()
                return@setOnClickListener
            }
        }

        viewContainer.add(view)
    }






    inner class ViewPagerAdapter : PagerAdapter() {


        override fun getCount(): Int {
            return viewContainer.size
        }

        override fun isViewFromObject(view: View, `object`: Any): Boolean {
            return view === `object`
        }

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            if (viewContainer[position].parent != null) {
                (viewContainer[position].parent as ViewPager).removeView(viewContainer[position])
            }
            container.addView(viewContainer[position], 0)
            return viewContainer[position]
        }

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            container.removeView(viewContainer[position])
        }
    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
    }

    override fun onPageSelected(position: Int) {

    }

    override fun onPageScrollStateChanged(state: Int) {
    }

    override fun onBackPressed() {
        if(isDoneFinish){
            super.onBackPressed()
        }
    }
}